package com.tencent.wxcloudrun.dto;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import com.tencent.wxcloudrun.domain.TQuartzJob;
import com.tencent.wxcloudrun.enums.JobStatEnum;
import com.tencent.wxcloudrun.schedule.*;
import com.tencent.wxcloudrun.util.DateUtils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Ryan Zhao
 *         <p>
 *         Copyright: Copyright (c) 2023
 *         </p>
 * @version 1.0
 * @date 2023-08-22 11:48
 * @description: 定时任务接口入参
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "定时任务接口入参")
public class QuartzJobDTO {

    @ApiModelProperty("业务序号")
    private Long busiId;

    @ApiModelProperty("业务类型")
    private Integer busiType;

    @ApiModelProperty("业务时间")
    private LocalDateTime busiTime;

    @ApiModelProperty("创建用户")
    private String userId;

    public TQuartzJob build() {
        TQuartzJob quartzJob = new TQuartzJob();
        quartzJob.setBusiId(this.getBusiId());
        quartzJob.setBusiType(this.getBusiType());
        quartzJob.setJobStat(JobStatEnum.SUCCESS.getValue());
        String jobClassName = getJobClassNameByBusiType(this.getBusiType());
        quartzJob.setJobClassName(jobClassName);
        quartzJob.setJobCron(getJobCron(this.getBusiTime()));
        quartzJob.setJobParam(String.valueOf(this.getBusiId()));
        quartzJob.setCreatedBy(this.getUserId());
        quartzJob.setCreatedTime(LocalDateTime.now());
        quartzJob.setUpdatedBy(this.getUserId());
        quartzJob.setUpdatedTime(LocalDateTime.now());
        return quartzJob;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/28
     * @Params: [busiType]
     * @Return: String
     * @Description: 获取任务类名
     */
    private String getJobClassNameByBusiType(Integer busiType) {
        switch (busiType) {
            case 1:
                return ActiBeginJob.class.getName();
            case 2:
                return ActiEndJob.class.getName();
            case 3:
                return ActiSelectedBeginJob.class.getName();
            case 4:
                return ActiSelectedEndJob.class.getName();
            case 5:
                return ActiBlackBeginJob.class.getName();
            case 6:
                return ActiBlackEndJob.class.getName();
            default:
                return "";
        }
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/28
     * @Params: [busiDate, busiTime]
     * @Return: String
     * @Description: 获取任务cron
     */
    public String getJobCron(Integer busiDate, Integer busiTime) {
        LocalDate date = LocalDate.parse(String.valueOf(busiDate), DateTimeFormatter.ofPattern("yyyyMMdd"));
        String busiTimeStr = DateUtils.suppleTime(String.valueOf(busiTime));
        LocalTime time = LocalTime.parse(busiTimeStr, DateTimeFormatter.ofPattern("HHmmss"));
        return time.getSecond() + " " + time.getMinute() + " " + time.getHour() + " " + date.getDayOfMonth() + " "
            + date.getMonth().getValue() + " ? " + date.getYear();
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/30
     * @Params: [busiTime]
     * @Return: String
     * @Description: 获取任务cron
     */
    public String getJobCron(LocalDateTime busiTime) {
        return busiTime.getSecond() + " " + busiTime.getMinute() + " " + busiTime.getHour() + " " + busiTime.getDayOfMonth() + " "
                + busiTime.getMonth().getValue() + " ? " + busiTime.getYear();
    }

}
